<template>
	<view class="setting-container" :style="{height: `${windowHeight}px`}">
		<view class="menu-list">
			<view class="list-cell list-cell-arrow" @click="handleToPwd">
				<view class="menu-item-box">
					<view class="iconfont icon-password menu-icon"></view>
					<view>修改密码</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleToUpgrade">
				<view class="menu-item-box">
					<view class="iconfont icon-refresh menu-icon"></view>
					<view>检查更新</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleCleanTmp">
				<view class="menu-item-box">
					<view class="iconfont icon-clean menu-icon"></view>
					<view>清理缓存</view>
				</view>
			</view>
		</view>
		<view class="cu-list menu">
			<view class="cu-item item-box">
				<view class="content text-center" @click="handleLogout">
					<text class="text-black">退出登录</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance,
	} from 'vue'
	import config from '@/config'

	import useUserStore from '@/store/modules/user'

	const userStore = useUserStore();
	const {
		proxy
	} = getCurrentInstance();
	const windowHeight = ref(0);
	const getWindowHeight = () => {
		windowHeight.value = uni.getSystemInfoSync().windowHeight
	}

	const handleToPwd = () => {
		proxy.$tab.navigateTo('/pages/mine/pwd/index')
	}
	const handleToUpgrade = () => {
		// proxy.$modal.showToast('模块建设中~')
		
		// #ifdef APP-PLUS  
		plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {  
		    uni.request({  
		        url: config.updateUrl,  
		        data: {  
		            version: widgetInfo.version,  
		            name: widgetInfo.name  
		        },  
		        success: (result) => {  
		            var data = result.data;  
		            if (data.update && data.wgtUrl) {  
		                uni.downloadFile({  
		                    url: data.wgtUrl,  
		                    success: (downloadResult) => {  
		                        if (downloadResult.statusCode === 200) {  
		                            plus.runtime.install(downloadResult.tempFilePath, {  
		                                force: false  
		                            }, function() {  
		                                // console.log('install success...');  
		                                plus.runtime.restart();  
		                            }, function(e) {  
		                                // console.error('install fail...');  
		                            });  
		                        }  
		                    }  
		                });  
		            }  
		        }  
		    });  
		});  
		// #endif
		
		// #ifdef MP
		const updateManager = uni.getUpdateManager();
		
		updateManager.onCheckForUpdate(function(res) {
			// 请求完新版本信息的回调
			// console.log(res.hasUpdate);
		});
		
		updateManager.onUpdateReady(function(res) {
			uni.showModal({
				title: '更新提示',
				content: '新版本已经准备好，是否重启应用？',
				success(res) {
					if (res.confirm) {
						// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
						updateManager.applyUpdate();
					}
				}
			});
		
		});
		
		updateManager.onUpdateFailed(function(res) {
			// 新的版本下载失败
		});
		// #endif
	}
	const handleCleanTmp = () => {
		proxy.$modal.confirm('确定清空缓存吗？登陆状态会失效。').then(res => {
			userStore.logOut().then(res => {
				uni.clearStorageSync()
				proxy.$tab.reLaunch('/pages/login')
			})
		})
		// proxy.$modal.showToast('模块建设中~')
	}
	const handleLogout = () => {
		proxy.$modal.confirm('确定注销并退出系统吗？').then(res => {
			userStore.logOut().then(res => {
				proxy.$tab.reLaunch('/pages/login')
			})
		})
	}

	getWindowHeight()
</script>

<style lang="scss" scoped>
	.item-box {
		background-color: #FFFFFF;
		margin: 30rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 10rpx;
		border-radius: 8rpx;
		color: #303133;
		font-size: 32rpx;
	}
</style>
<style lang="scss">
	//仅设置背景，不推荐在这里写样式代码
	page {
		background-color: #f8f8f8;
	}
</style>